<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      html {
        scroll-behavior: smooth;
      }
      #box1 {
        margin: 0 auto;
        margin-top: 400px;
        width: 500px;
        height: 300px;
        background-color: skyblue;
      }
      #box2 {
        position: fixed;
        right: 50px;
        bottom: 50px;
        width: 50px;
        height: 50px;
        background-color: hotpink;
        display: none;
      }
    </style>
    <script src="../jquery.min.js"></script>
    <script>
      $(function () {
        $(document).on('scroll', function () {
          const scrollTop = $(this).scrollTop()
          console.log(scrollTop, 'scrollTop')
          const box1OffsetTop = $('#box1').offset().top
          if (scrollTop > box1OffsetTop) {
            $('#box2').fadeIn()
          } else {
            $('#box2').fadeOut()
          }
        })

        // 能用css实现的动画，尽量不用js去做
        $('#box2').on('click', function () {
          $('html').scrollTop(0)
        })
        // $('#box2').on('click', function () {
        //   $('html').animate({ scrollTop: 0 }, 300)
        // })
      })
    </script>
  </head>
  <body>
    <div id="box1"></div>

    <div id="box2">返回顶部</div>

    <br />
    <br />
    <br /><br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br /><br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br /><br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />
  </body>
</html>
